<?php

namespace App\Models\Article;

use App\Models\BaseModel;
use App\Models\Traits\KfSoftDeletes;
use App\Models\Traits\KfUid;
use Illuminate\Database\Eloquent\Factories\HasFactory;

class ArticleStatisticsLogsModel extends BaseModel
{
    use HasFactory;
    use KfUid;
    use KfSoftDeletes;

    protected $table = "article_statistics_logs";

    protected $fillable = [
        'id',
        'articleId',
        'type',
        'content',
        'type',
        'isDeleted',
        'createdAt',
        'createdByUid',
        'updatedAt',
        'updatedByUid',
    ];


    const SOURCE_ARTICLE  = 0;
    const SOURCE_CATEGORY = 1;
    const SOURCE_TEXT     = [
        self::SOURCE_ARTICLE  => "文章",
        self::SOURCE_CATEGORY => "专题"
    ];

    const TYPE_LIKE           = 1;
    const TYPE_CANCEL_LIKE    = 2;
    const TYPE_COLLECT        = 3;
    const TYPE_CANCEL_COLLECT = 4;
    const TYPE_SHARE          = 5;
    const TYPE_READ           = 6;
    const TYPE_SHOW           = 7;

    const TYPE_TEXT = [
        self::TYPE_LIKE           => "点赞",
        self::TYPE_CANCEL_LIKE    => "取消点赞",
        self::TYPE_COLLECT        => "收藏",
        self::TYPE_CANCEL_COLLECT => "取消收藏",
        self::TYPE_SHARE          => "分享转发",
        self::TYPE_READ           => "阅读",
        self::TYPE_SHOW           => "展示",
    ];

    const TYPE_CANCEL_TEXT = [
        self::TYPE_CANCEL_LIKE    => "取消点赞",
        self::TYPE_CANCEL_COLLECT => "取消收藏",
    ];

    public static function add(array $data)
    {
        return self::query()->create($data);
    }

    public static function getCount(int $type, int $startTime, int $endTime)
    {
        return ArticleStatisticsLogsModel::query()
                                         ->where('type', $type)
                                         ->whereBetween('createdAt', [$startTime, $endTime])
                                         ->groupBy('articleId')
                                         ->selectRaw("articleId,type,count(1) num")
                                         ->get();
    }

    /**
     * 获取对应文章表的字段
     * @param int $type
     * @return string
     */
    public static function getColumn(int $type): string
    {
        switch ($type) {
            case self::TYPE_LIKE:
            case self::TYPE_CANCEL_LIKE:
                $column = 'supportNum';
                break;
            case self::TYPE_COLLECT:
            case self::TYPE_CANCEL_COLLECT:
                $column = 'collectNum';
                break;
            case self::TYPE_SHARE:
                $column = 'forwardNum';
                break;
            case self::TYPE_READ:
                $column = 'readNum';
                break;
            case self::TYPE_SHOW:
                $column = 'showNum';
                break;
            default:
                $column = '';
        }
        return $column;
    }


    /**
     * 获取对应统计表来源
     * @param $type
     * @return int
     */
    public static function getSource($type): int
    {
        switch ($type) {
            case self::TYPE_LIKE:
            case self::TYPE_CANCEL_LIKE:
                $source = ArticleStatisticsDayModel::TYPE_LIKE;
                break;
            case self::TYPE_COLLECT:
            case self::TYPE_CANCEL_COLLECT:
                $source = ArticleStatisticsDayModel::TYPE_COLLECT;
                break;
            case self::TYPE_SHARE:
                $source = ArticleStatisticsDayModel::TYPE_SHARE;
                break;
            case self::TYPE_READ:
                $source = ArticleStatisticsDayModel::TYPE_READ;
                break;
            case self::TYPE_SHOW:
                $source = ArticleStatisticsDayModel::TYPE_SHOW;
                break;
            default:
                $source = 0;
        }
        return $source;
    }

}
